{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 可变序列、不可变序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2791804089224\n",
      "2791804089224\n",
      "2791803792328\n",
      "helloworld\n",
      "2791803742832\n"
     ]
    }
   ],
   "source": [
    "# 可变序列：列表，字典\n",
    "lst = [10, 20, 45]\n",
    "print(id(lst))\n",
    "lst.append(300)\n",
    "print(id(lst))  # 内存地址未更改\n",
    "\n",
    "# 不可变序列：字符串，元组\n",
    "s = 'hello'\n",
    "print(id(s))\n",
    "s = s + 'world'\n",
    "print(s)\n",
    "print(id(s))  # 内存地址更改"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 元组创建方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Python', 'world', 98)\n",
      "<class 'tuple'>\n",
      "('Python', 'world', 98)\n",
      "<class 'tuple'>\n",
      "('Python',)\n",
      "<class 'tuple'>\n",
      "('Python', 'world', 98)\n",
      "<class 'tuple'>\n",
      "空元组： () ()\n",
      "空列表： [] []\n",
      "空字典： {} {}\n"
     ]
    }
   ],
   "source": [
    "# 第一种，使用()\n",
    "t = ('Python', 'world', 98)\n",
    "print(t)\n",
    "print(type(t))\n",
    "t2 = 'Python', 'world', 98  # ()可以省略\n",
    "print(t2)\n",
    "print(type(t2))\n",
    "t3 = ('Python',)  # 只包含一个元组的元素需要使用逗号和小括号，不然会以为是string类型\n",
    "print(t3)\n",
    "print(type(t3))\n",
    "\n",
    "# 第二种，使用内置函数tuple()\n",
    "t1 = tuple(('Python', 'world', 98))\n",
    "print(t1)\n",
    "print(type(t1))\n",
    "\n",
    "# 空元组的创建方式\n",
    "t4 = ()\n",
    "t5 = tuple()\n",
    "print('空元组：', t4, t5)\n",
    "# 空列表的创建方式\n",
    "lst = []\n",
    "lst1 = list()\n",
    "print('空列表：', lst, lst1)\n",
    "# 空字典的创建方式\n",
    "d = {}\n",
    "d1 = dict()\n",
    "print('空字典：', d, d1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 元组不可变序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10, [20, 30], 9)\n",
      "<class 'tuple'>\n",
      "10 <class 'int'> 1653236192\n",
      "[20, 30] <class 'list'> 2791804092296\n",
      "9 <class 'int'> 1653236160\n",
      "1653239072\n",
      "(10, [20, 30, 100], 9) 2791804092296\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "为什么要将元组设计成不可变序列：\n",
    "在多任务环境下，同时操作对象时不需要加锁，因此在程序中尽量使用不可变序列\n",
    "\"\"\"\n",
    "\"\"\"\n",
    "注意：元组中存储的是对象的引用\n",
    "1.如果元组中的对象本身不可变对象，则不能再引用其他对象\n",
    "2.如果元组中的对象是可变对象，则可变对象的引用不允许改变，但数据可以改变\n",
    "\"\"\"\n",
    "t = (10, [20, 30], 9)\n",
    "print(t)\n",
    "print(type(t))\n",
    "print(t[0], type(t[0]), id(t[0]))\n",
    "print(t[1], type(t[1]), id(t[1]))\n",
    "print(t[2], type(t[2]), id(t[2]))\n",
    "# 尝试将t[1]修改为100\n",
    "print(id(100))\n",
    "# t[1] = 100  # TypeError: 'tuple' object does not support item assignment\n",
    "# 由于[20,30]是列表，列表是可变序列，所以可以将列表中添加元素，而列表的内存地址不变\n",
    "t[1].append(100)\n",
    "print(t, id(t[1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 元组遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python\n",
      "world\n",
      "98\n",
      "Python\n",
      "world\n",
      "98\n"
     ]
    }
   ],
   "source": [
    "t = ('Python', 'world', 98)\n",
    "# 第一种获取元组元素的方式，索引\n",
    "# 需要已知元素有多少\n",
    "print(t[0])\n",
    "print(t[1])\n",
    "print(t[2])\n",
    "# print(t[3])  # IndexError: tuple index out of range\n",
    "# 遍历元组\n",
    "for item in t:\n",
    "    print(item)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
